package com.sanjiahantang.youjiaclient.module.programmeprice.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.OrderActivity;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.ServiceOrderActivity;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramAddOrderListBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.beans.ProgramOrderListBean;
import com.sanjiahantang.youjiaclient.module.programmeprice.presenters.ProgramPricePresenter;
import com.sanjiahantang.youjiaclient.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * 增项报价清单
 * Created by zy on 2017/11/21.
 */

public class ProgramAddOrderActivity extends BaseMvpActivity<ProgramPriceView, ProgramPricePresenter> implements ProgramPriceView, View.OnClickListener {

    private View panel1, panel2, panel3, panel4;

    private TextView txtPlanPrice, txtOrderPrice;
    private ProgramAddOrderListBean.Data orderData;
    private ListView listIncrease;
    private String ordercode;
    private int type;

    @Override
    public ProgramPricePresenter initPresenter() {
        return new ProgramPricePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_program_add_order_list);
        ordercode = getIntent().getStringExtra(KEY_ID);
        type = getIntent().getIntExtra(KEY_TYPE, 0);
//        if (ordercode==null){
//            Utils.showToast($this,"订单找不到了");
//            finish();
//            return;
//        }
        txtPlanPrice = (TextView) findViewById(R.id.txtPlanPrice);
        txtOrderPrice = (TextView) findViewById(R.id.txtOrderPrice);
        panel1 = findViewById(R.id.panel1);
        panel2 = findViewById(R.id.panel2);
        panel3 = findViewById(R.id.panel3);
        panel4 = findViewById(R.id.panel4);

        panel1.setVisibility(View.GONE);
        panel2.setVisibility(View.GONE);
        panel3.setVisibility(View.GONE);
        panel4.setVisibility(View.GONE);
        listIncrease = (ListView) findViewById(R.id.listIncrease);


        TextView title = (TextView) findViewById(R.id.title);
        if (type == TYPE_INCREASE) {
            title.setText("增项报价清单");
        }

        findViewById(R.id.fl_Left).setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.addPriceLoadBaoJList($this, ordercode, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {

                if (content != null && content instanceof ProgramAddOrderListBean.Data) {
                    orderData = (ProgramAddOrderListBean.Data) content;
                    // txtPlanPrice.setText(String.valueOf(orderData.getPlanPrice()));
                    for (int i = 0; i < orderData.getOrderServiceRaDtoList().size(); i++) {
                        fulfillPanel(panel1, orderData.getOrderServiceRaDtoList().get(i), "施工", R.layout.item_program_nopic_n);
                        panel1.setVisibility(View.VISIBLE);
                    }
                    //txtOrderPrice.setText(String.valueOf(orderData.getPlanInfo().getOrderTotalMoney()));
                    for (int i = 0; i < orderData.getOrderInfoGoodsRaDtoList().size(); i++) {
                        if (orderData.getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 0) {
                            fulfillPanel(panel2, orderData.getOrderInfoGoodsRaDtoList().get(i), "主材", R.layout.item_program_nopic_p);
                            panel2.setVisibility(View.VISIBLE);
                        } else if (orderData.getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 1) {
                            fulfillPanel(panel3, orderData.getOrderInfoGoodsRaDtoList().get(i), "家具", R.layout.item_program_nopic_p);
                            panel3.setVisibility(View.VISIBLE);
                        } else if (orderData.getOrderInfoGoodsRaDtoList().get(i).getOrderType() == 2) {
                            fulfillPanel(panel4, orderData.getOrderInfoGoodsRaDtoList().get(i), "装饰", R.layout.item_program_nopic_p);
                            panel4.setVisibility(View.VISIBLE);
                        }
                    }

                }
            }
        });
    }

    private void fulfillPanel(View itemView, final ProgramAddOrderListBean.Order order, String title, int resId) {
        TextView txtOrderCode = (TextView) itemView.findViewById(R.id.comName);
        TextView txtState = (TextView) itemView.findViewById(R.id.txtState);
        ListView itemList = (ListView) itemView.findViewById(R.id.SG_list);
        TextView txtCount = (TextView) itemView.findViewById(R.id.TotalCount);
        TextView txtPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
        itemView.findViewById(R.id.iv_titleIcon).setVisibility(View.GONE);


        txtState.setVisibility(View.VISIBLE);
        itemView.findViewById(R.id.countItem).setVisibility(View.VISIBLE);

        String stateName = order.getStatusName();
        switch (order.getStatusId()) {
            case OrderView.TYPE_UN_PAY:
                stateName = "待付款";
                itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                itemView.findViewById(R.id.btnEvaluate).setVisibility(View.GONE);
                break;
            case OrderView.TYPE_PART_PAY:
                itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                itemView.findViewById(R.id.btnCancel).setVisibility(View.GONE);
                itemView.findViewById(R.id.btnEvaluate).setVisibility(View.GONE);
                break;
            default:
                itemView.findViewById(R.id.panelAction).setVisibility(View.GONE);
                break;
        }


        txtOrderCode.setText(title + "  ( " + order.getOrderCode() + " )");
        txtState.setText(stateName);

        if (order.getServiceRecordList() != null) {

            if (order.getStatusId() == OrderView.TYPE_PAYED){
                itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                itemView.findViewById(R.id.btnCancel).setVisibility(View.GONE);
                itemView.findViewById(R.id.btnPay).setVisibility(View.GONE);
                itemView.findViewById(R.id.btnEvaluate).setVisibility(View.VISIBLE);
            }

            txtCount.setText(String.valueOf(order.getServiceRecordList().size()));
            txtPrice.setText(" ¥ " + String.valueOf(order.getPayMoney()));
            itemList.setAdapter(new QuickAdapter<ProgramAddOrderListBean.Service>($this, resId, order.getServiceRecordList()) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProgramAddOrderListBean.Service item) {
                    helper.getView(R.id.iv_pic).setVisibility(View.GONE);
                    helper.setText(R.id.sg_name, item.getServiceName());
                    helper.setText(R.id.txtPrice, "¥ " + String.valueOf(item.getPrice()));
                    helper.setText(R.id.txtCount, "x " + String.valueOf(item.getCount()));
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle b = new Bundle();
                            ServiceListBean.Data goods = new ServiceListBean.Data();
                            goods.setOrderCode(order.getOrderCode());
                            goods.setStatusName(order.getStatusName());
                            goods.setStatusId(order.getStatusId());
                            b.putSerializable("goods", goods);
                            Intent intent = new Intent($this, ServiceOrderActivity.class).putExtra(KEY_PARAM, b);
                            intent.putExtra("isFormPlan", 1);
                            startActivity(intent);
                        }
                    });
                }
            });

            itemView.findViewById(R.id.btnPay).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ServiceListBean.Data goods = new ServiceListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    goods.setStatusName(order.getStatusName());
                    goods.setStatusId(order.getStatusId());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, ServiceOrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });

            itemView.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ServiceListBean.Data goods = new ServiceListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, ServiceOrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });

            itemView.findViewById(R.id.btnEvaluate).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ServiceListBean.Data goods = new ServiceListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, ServiceOrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });

        }

        if (order.getGoodsInfoDtoList() != null) {

            if (order.getStatusId() == OrderView.TYPE_COMPLETE){
                itemView.findViewById(R.id.panelAction).setVisibility(View.VISIBLE);
                itemView.findViewById(R.id.btnCancel).setVisibility(View.GONE);
                itemView.findViewById(R.id.btnPay).setVisibility(View.GONE);
                itemView.findViewById(R.id.btnEvaluate).setVisibility(View.VISIBLE);
            }


            int count = 0;
            for (ProgramAddOrderListBean.Goods item : order.getGoodsInfoDtoList()){
                count += item.getCount();
            }
            txtCount.setText(String.valueOf(count));
            txtPrice.setText(" ¥ " + String.valueOf(order.getPayMoney()));

            final QuickAdapter adapter = new QuickAdapter<ProgramAddOrderListBean.Goods>($this, resId) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProgramAddOrderListBean.Goods item) {
                    helper.getView(R.id.iv_icon).setVisibility(View.GONE);
                    helper.setText(R.id.txtMainName, item.getRecordName());
                    helper.setText(R.id.txtMainPrice, "¥ " + String.valueOf(item.getPrice()));
                    helper.setText(R.id.txtCount, " " + String.valueOf(item.getCount()));


                    if (item.getMgpiValue() != null) {
                        TagGroup tagGroup = helper.getView(R.id.txtName);
                        tagGroup.setTags(item.getMgpiValue().split(","));
                    }

                    ImageView iv_pic = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl()))
                            .placeholder(RES_ID_GOODS_DEFAULT)
                            .into(iv_pic);

                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle b = new Bundle();
                            ShopListBean.Data goods = new ShopListBean.Data();
                            goods.setOrderCode(order.getOrderCode());
                            b.putSerializable("goods", goods);
                            Intent intent = new Intent($this, OrderActivity.class).putExtra(KEY_PARAM, b);
                            intent.putExtra("isFormPlan", 1);
                            startActivity(intent);
                        }
                    });
                }
            };
            List<ProgramAddOrderListBean.Goods> showList = new ArrayList<>();
            if (order.getGoodsInfoDtoList() != null){
                final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
                if (order.getGoodsInfoDtoList().size() > SHOW_SIZE){
                    showList.addAll(order.getGoodsInfoDtoList().subList(0,SHOW_SIZE));
                    tvMore.setVisibility(View.VISIBLE);
                    tvMore.setTag(R.id.decode,1);
                    tvMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Object tag = view.getTag(R.id.decode);
                            if (tag != null && tag instanceof Integer){
                                int type = (int) tag;
                                if (type == 1){
                                    tvMore.setTag(R.id.decode,2);
                                    tvMore.setText("收起列表");
                                    adapter.clear();
                                    adapter.addAll(order.getGoodsInfoDtoList());
                                }else{
                                    tvMore.setTag(R.id.decode,1);
                                    tvMore.setText("查看全部");
                                    adapter.clear();
                                    adapter.addAll(order.getGoodsInfoDtoList().subList(0,SHOW_SIZE));
                                }
                            }
                        }
                    });
                }else{
                    showList.addAll(order.getGoodsInfoDtoList());
                    tvMore.setVisibility(View.GONE);
                }
            }
            adapter.clear();
            adapter.addAll(showList);
            itemList.setAdapter(adapter);

            itemView.findViewById(R.id.btnPay).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ShopListBean.Data goods = new ShopListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, OrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });

            itemView.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ShopListBean.Data goods = new ShopListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, OrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });


            itemView.findViewById(R.id.btnEvaluate).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    ShopListBean.Data goods = new ShopListBean.Data();
                    goods.setOrderCode(order.getOrderCode());
                    b.putSerializable("goods", goods);
                    Intent intent = new Intent($this, OrderActivity.class).putExtra(KEY_PARAM, b);
                    intent.putExtra("isFormPlan", 1);
                    startActivity(intent);
                }
            });

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
        }
    }

    @Override
    public void showProgress() {

    }
}
